package top.songxs.gmall.rabbit.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.songxs.gmall.rabbit.constant.MqConstant;

@Configuration
public class RabbitMqConfiguration {

    @Bean
    public Exchange orderExchange() {  //声名订单所需交换机
        Exchange exchange = ExchangeBuilder
                .directExchange(MqConstant.ORDER_EXCHANGE_NAME)//指定交换机类型和交换机名称
                .durable(true)  //指定交换机的持久化
                .build();

        return exchange;
    }

    @Bean
    public Queue orderQueue() {
        Queue queue = QueueBuilder.durable(MqConstant.ORDER_QUEUE_NAME) //指定队列名称，持久化队列
                .ttl(MqConstant.ORDER_TTL)  //指定消息的存活时间
                .deadLetterExchange(MqConstant.ORDER_EXCHANGE_NAME) //指定死信交换机
                .deadLetterRoutingKey(MqConstant.DEAD_LETTER_MSG_ROUTING_KEY)   //指定死信队列路由键
                .build();

        return queue;

    }

    //  声名订单消息队列和订单交换机的绑定信息
    @Bean
    public Binding orderQueueBinding() {

        Binding binding = BindingBuilder
                .bind(orderQueue())  //指定绑定队列
                .to(orderExchange())  //指定绑定交换机
                .with(MqConstant.ORDER_MSG_ROUTING_KEY)  //指定绑定所需要的路由键
                .noargs();

        return binding;
    }


    //死信队列
    @Bean
    public Queue closeQueue() {
        Queue queue = QueueBuilder.durable(MqConstant.CLOSE_QUEUE_NAME) //指定死信队列，持久化队列
                .build();

        return queue;

    }

    //声名死信队列和死信交换机的绑定
    @Bean
    public Binding closeQueueBinding() {

        Binding binding = BindingBuilder
                .bind(closeQueue())  //指定绑定队列
                .to(orderExchange())  //指定绑定交换机
                .with(MqConstant.DEAD_LETTER_MSG_ROUTING_KEY)  //指定死信队列绑定所需要的路由键
                .noargs();
        return binding;
    }

    @Bean
    public Queue payedQueue() {
        return QueueBuilder.durable(MqConstant.PAYED_QUEUE).build();
    }

    @Bean
    public Binding payedQueueBinding() {
        return BindingBuilder.bind(payedQueue()).to(orderExchange()).with(MqConstant.PAYED_ROUTING_KEY).noargs();
    }


}
